<?php

class Api_Util_Encryption_Hashing extends Api_Util_Abstract
{
    /**
     * Salt lentgh
     */
    const SALT_LENGTH = 15;
    
    /**
     * Salt key which is added to the value 
     */
    const KEY = '!@#$%^&*()_+=-{}][;";/?<>.,';
    
    public function __construct()
    {
        
    }
   
    /**
     * Genrate salt value
     * 
     * @return string
     */
    public function getSalt()
    {
        $salt = null;
        if (!Zend_Registry::isRegistered('salt'))
            $salt = $this->createSalt();
        else
            $salt = Zend_Registry::get('salt');
        return $salt;
    }

    private function createSalt()
    {
        $salt = substr(hash('sha512',uniqid(rand(), true) .
                self::KEY . microtime()), 0, self::SALT_LENGTH);
        Zend_Registry::set('salt', $salt);
        return $salt;
    }

    /**
     * Hashin value with salt
     * 
     * @param string $password
     * @param string $salt
     * @return string 
     */
    public function hashPassword($password, $salt = null)
    {
        if ($salt == '' || $salt == null) {
            $salt = $this->getSalt();
        } else {
            $salt = substr($salt, 0, self::SALT_LENGTH);
        }
        return hash('sha512', $salt . self::KEY .  $password);
    }

    public static function generateActivationKey()
    {
        return hash('sha512',uniqid(rand(), true) .
                self::KEY . microtime());
    }

}